package com.lin.mybatis.jsql;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.Alias;
import net.sf.jsqlparser.expression.JdbcParameter;
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.select.AllColumns;
import net.sf.jsqlparser.statement.select.PlainSelect;
import org.junit.jupiter.api.Test;

import java.util.Arrays;

/**
 * @author linjiang
 * @date 2024/6/14
 * @fn
 */
public class TestUse {
    @Test
    public void test() throws JSQLParserException {
        String expectSql = "select * from user u where u.id = ?";

        // 构建表
        Table user = new Table("user");
        Alias alias = new Alias("u");
        user.setAlias(alias);

        // 构建表达式
        Column column = new Column();
        column.setTable(user);
        column.setColumnName("id");

        JdbcParameter parameter = new JdbcParameter(0,false);

        EqualsTo equalsTo = new EqualsTo();
        equalsTo.setLeftExpression(column);
        equalsTo.setRightExpression(parameter);

        PlainSelect plainSelect = new PlainSelect();
        plainSelect.setWhere(equalsTo);
        plainSelect.setFromItem(user);
        plainSelect.setSelectItems(Arrays.asList(new AllColumns()));

        System.out.println(plainSelect);

    }
}
